/**
 * 商品类别数据模块
 */
import axios from "../../utils/axios";
import {Message} from "element-ui"

export default {
    namespaced: true,
    state: {
        cateList: [],
    },
    mutations: {
        initCateListMutations(state,list) {
            state.cateList = list
        }
    },
    actions: {
        initCateListActions({commit}) {
            axios.get("/catelist?istree=1").then(response => {
                if(response.code === 200) {
                    console.log(response);
                    commit("initCateListMutations", response.list)
                }
            })
        },
        addCateListActions({dispatch},cate) {
            console.log("addCateListActions 函数执行了", cate)
            axios.post("/cateadd",cate).then(response => {
                console.log(response)
                if(response.code === 200) {
                    Message.success("新的类型添加成功")
                    dispatch("initCateListActions")
                }
            })

                // 第二种请求数据的转换方式，了解即可
                // axios({
                //     url: "/cateadd",
                //     method: "POST",
                //     data: cate,
                //     transformRequest: [function(data) {
                //         let ret;
                //         for(let key in data) {
                //             ret += encodeURIComponent(key) + "=" + encodeURIComponent(data[key]) + "&"
                //         }
                //         return ret
                //     }],
                //     headers: {"Content-Type": "application/x-www.-form-urlencoded"}
                // }).then(response => {
                //     if(response.code === 200) {
                //         dispatch("initCateListActions")
                //     }
                // })
        },
        editCateListActions({dispatch},cate) {
            axios.post("/cateedit",cate).then(response => {
                console.log(response);
                if(response.code === 200) {
                    Message.success("类型编辑成功")
                    dispatch("initCateListActions")
                }
            })
        },
        deleteCateListActions({dispatch},id) {
            axios.post("/catedelete",{id}).then(response => {
                console.log(response);
                if(response.code === 200) {
                    Message.success("类型删除成功")
                    dispatch("initCateListActions")
                }
            })
        },
    },
    getters: {}
}
